package com.ymttest.business.service.shenqi;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;

import org.apache.http.client.ClientProtocolException;

import com.shop2cn.iapi.openapibiz.parameter.AuthorizeBean;
import com.shop2cn.iapi.openapibiz.parameter.CancelAuthBean;
import com.shop2cn.iapi.openapibiz.parameter.NewThirdPartyAppBean;
import com.shop2cn.iapi.openapibiz.service.AuthorizeCall;
import com.shop2cn.iapi.openapibiz.service.CancelAuthCall;
import com.shop2cn.iapi.openapibiz.service.NewThirdPartyAppCall;
import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqProductExtInfo;
import com.ymatou.apigateway.parameter.SqPushSqOrderCustomsClearanceStatusReq;
import com.ymatou.apigateway.parameter.SqPushSqOrderExtInfoReq;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqPushSqOrderCustomsClearanceStatusCall;
import com.ymatou.apigateway.service.SqPushSqOrderExtInfoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class OpenApiBizCallService {
	
	private static OpenapiReq openapiReq = new OpenapiReq();
	
	private static OpenApiWapper openApiWapper = new OpenApiWapper();
	
	/// 创建第三方应用app
	public NewThirdPartyAppBean createNewThirdPartyAppBean()
	{
		NewThirdPartyAppBean newThirdPartyAppBean = new NewThirdPartyAppBean();
		newThirdPartyAppBean.setAppName("第三方应用"+TestDataManager.getRandomNum(5));
		newThirdPartyAppBean.setAppDesc("dscjjkjjkk");
		return newThirdPartyAppBean;
	}
	
	public NewThirdPartyAppCall newthirdpartyappCall(NewThirdPartyAppBean newThirdPartyAppBean) throws ClientProtocolException, IOException
	{
		NewThirdPartyAppCall newThirdPartyAppCall = new NewThirdPartyAppCall();
		
		newThirdPartyAppCall.setData(newThirdPartyAppBean);
		newThirdPartyAppCall.callService();			
	
		return newThirdPartyAppCall;
	}
	
	///授权： 卖手给应用授权
	public AuthorizeBean createAuthorizeBean_USER_DEV()
	{
		AuthorizeBean authorizeBean = new AuthorizeBean();
		
		String appName="auto_USER_DEV_"+TestDataManager.getRandomIntNum(6);
		   
		authorizeBean.setSellerId(TestDataManager.getRandomIntNum(6));
		authorizeBean.setAppName(appName);
		authorizeBean.setType("USER_DEV");
		authorizeBean.setAppDesc("APP_Desc "+TestDataManager.getRandomIntNum(10));

		return authorizeBean;
	}
	
	public AuthorizeBean createAuthorizeBean_THIRD_PARTY(String appId)
	{
		AuthorizeBean authorizeBean = new AuthorizeBean();
		
		Integer userId=	TestDataManager.getRandomIntNum(5);
		   
		authorizeBean.setSellerId(userId);
		authorizeBean.setAppId(appId);
		authorizeBean.setType("THIRD_PARTY");
		authorizeBean.setAppDesc("APP_Desc "+TestDataManager.getRandomIntNum(10));

		return authorizeBean;
	}
	
	public AuthorizeCall authorizeCall(AuthorizeBean authorizeBean) throws ClientProtocolException, IOException
	{
		AuthorizeCall authorizeCall = new AuthorizeCall();
		
		authorizeCall.setData(authorizeBean);
		authorizeCall.callService();		
		
		return authorizeCall;
	}
	
	//取消授权
	public CancelAuthBean createCancelAuthBean(Integer sellerId, String authCode, String appId)
	{
		CancelAuthBean cancelAuthBean = new CancelAuthBean();
		
		cancelAuthBean.setSellerId(sellerId);
		cancelAuthBean.setAuthCode(authCode);
		cancelAuthBean.setAppId(appId);
		
		return cancelAuthBean;
	}
	
	public CancelAuthCall cancelAuth(CancelAuthBean cancelAuthBean) throws ClientProtocolException, IOException
	{
		CancelAuthCall cancelAuthCall = new CancelAuthCall();
		
		cancelAuthCall.setData(cancelAuthBean);
		cancelAuthCall.callService();
		
		return cancelAuthCall;
	}
	
	@SuppressWarnings("static-access")
	public static void SqPushSqOrderCustomsClearanceStatus(Long orderId, Long sellerId) throws Exception {
		
		SqPushSqOrderCustomsClearanceStatusReq sqPushSqOrderCustomsClearanceStatusReq = new SqPushSqOrderCustomsClearanceStatusReq();
		SqPushSqOrderCustomsClearanceStatusCall sqPushSqOrderCustomsClearanceStatusCall = new SqPushSqOrderCustomsClearanceStatusCall();
		
		sqPushSqOrderCustomsClearanceStatusReq.setOrder_id(orderId);
		sqPushSqOrderCustomsClearanceStatusReq.setCustoms_clearance_status(2);
		sqPushSqOrderCustomsClearanceStatusReq.setCustoms_clearance_status_desc("海关通关中");

		Application application = openApiWapper
				.selectApplicationBySellerId(sellerId).get(0);

		String app_id = application.getApp_id();

		AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
				.get(0);

		openapiReq.setApp_id(app_id);

		String app_secret = application.getApp_secret();

		openapiReq.setMethod("sq.customs.clearance.status.push");
		openapiReq.setSign_method("MD5");
		openapiReq.setAuth_code(authCode.getAuth_code());
		openapiReq.setTimestamp(YMTDateUtil.getSysTime());
		openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
		openapiReq.setBiz_content(new FormatUtil()
				.beanToGSONString(sqPushSqOrderCustomsClearanceStatusReq));
		String url = String
				.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.customs.clearance.status.push",
						app_id);

		sqPushSqOrderCustomsClearanceStatusCall.setUrl(url);

		openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
				AesUtil.decrypt(app_secret)));

		sqPushSqOrderCustomsClearanceStatusCall.setData(openapiReq);
		sqPushSqOrderCustomsClearanceStatusCall.callService();
	}
	
	@SuppressWarnings("static-access")
	public static void sqPushSqOrderExtInfo(Long orderId, Long sellerId, String outerSkuId) throws Exception {
		
		SqPushSqOrderExtInfoReq sqPushSqOrderExtInfoReq = new SqPushSqOrderExtInfoReq();
		SqPushSqOrderExtInfoCall sqPushSqOrderExtInfoCall = new SqPushSqOrderExtInfoCall();
		
		SqProductExtInfo sqProductExtInfo = new SqProductExtInfo();
		
		sqProductExtInfo.setOuter_sku_id(outerSkuId);
		sqProductExtInfo.setCustoms_tax_fee(BigDecimal.TEN);
		sqProductExtInfo.setCustoms_tax_rate(new BigDecimal("0.2881"));
		sqProductExtInfo.setGoods_supplier("海鲲专供");

		sqPushSqOrderExtInfoReq.setOrder_id(orderId);
		sqPushSqOrderExtInfoReq.setProduct_ext_infos(Arrays.asList(sqProductExtInfo));

		Application application = openApiWapper
				.selectApplicationBySellerId(sellerId).get(0);

		String app_id = application.getApp_id();

		AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
				.get(0);

		openapiReq.setApp_id(app_id);

		String app_secret = application.getApp_secret();

		openapiReq.setMethod("sq.orderextinfo.push");
		openapiReq.setSign_method("MD5");
		openapiReq.setAuth_code(authCode.getAuth_code());
		openapiReq.setTimestamp(YMTDateUtil.getSysTime());
		openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
		openapiReq.setBiz_content(new FormatUtil()
				.beanToGSONString(sqPushSqOrderExtInfoReq));
		String url = String
				.format("http://apigateway.shop2cn.com/apigateway/v1?app_id=%s&method=sq.orderextinfo.push",
						app_id);

		sqPushSqOrderExtInfoCall.setUrl(url);

		openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
				AesUtil.decrypt(app_secret)));

		sqPushSqOrderExtInfoCall.setData(openapiReq);
		sqPushSqOrderExtInfoCall.callService();
	}
	
	public static void main(String[] args) {
		try {
//			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
//			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_USER_DEV();
//			authorizeBean.setSellerId(288484);
//			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);

			
//			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
//			
//			NewThirdPartyAppBean newThirdPartyAppBean = OpenApiBizCallService.createNewThirdPartyAppBean();
//			NewThirdPartyAppCall newThirdPartyAppCall = OpenApiBizCallService.newthirdpartyappCall(newThirdPartyAppBean);
//	
//			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_THIRD_PARTY(newThirdPartyAppCall.getAppId());
//			authorizeBean.setSellerId(288484);
//			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);
			
			OpenApiBizCallService.SqPushSqOrderCustomsClearanceStatus(13133818699L, 500002490L);

		} catch (Exception e) {
			// TODO: handle exception
		}
	
	}
}
